package com.zenika.zenbinding.client.widget;

import java.util.List;

import com.google.gwt.user.client.ui.Grid;
import com.zenika.zenbinding.client.model.BusinessObject;
import com.zenika.zenbinding.client.model.ListModel;
import com.zenika.zenbinding.client.model.ValueModel;
import com.zenika.zenbinding.client.model.ZenEnum;

public class MultiColumnUserFormPanel extends Grid {
  
  protected VerticalUserFormPanel[] subpanels;
  protected int nbInputRows;
  protected int currentInputRow;
  
  public MultiColumnUserFormPanel(int nbInputColumns, int nbInputRows) {
    this(createSubPanels(nbInputColumns, nbInputRows), nbInputRows);
  }
  
  public MultiColumnUserFormPanel(VerticalUserFormPanel[] subpanels, int nbInputRows) {
    super(1, subpanels.length);
    // FIXME DEBUG
    this.setBorderWidth(1);
    this.nbInputRows = nbInputRows;
    this.currentInputRow = 0;
    
    this.subpanels = subpanels;
    for ( int i = 0; i < this.subpanels.length; i++ ) {
      this.setWidget(0, i, this.subpanels[i]);
    }
  }
  
  protected static VerticalUserFormPanel[] createSubPanels(int count, int nbInputRows) {
    VerticalUserFormPanel[] result = new VerticalUserFormPanel[count];
    for ( int i = 0; i < count; i++ ) {
      result[i] = new VerticalUserFormPanel(nbInputRows);
    }
    
    return result;
  }
  
  public ValueModel addTextBox(BusinessObject object, String property, String label) {
    return this.getCurrentPanelAndUseLine().addTextBox(object, property, label);
  }
  
  public ValueModel addTextArea(BusinessObject object, String property, String label) {
    return this.getCurrentPanelAndUseLine().addTextArea(object, property, label);
  }
  
  public ValueModel addCheckBox(BusinessObject object, String property, String label) {
    return this.getCurrentPanelAndUseLine().addCheckBox(object, property, label);
  }
  
  public ListModel addRadioGroup(BusinessObject businessObject, String property, List<BusinessObject> businessObjects, String propertyInList, String label, boolean isVertical) {
    return this.getCurrentPanelAndUseLine().addRadioGroup(businessObject, property, businessObjects, propertyInList, label, isVertical);
  }
  
  public ListModel addRadioGroup(BusinessObject businessObject, String property, BusinessObject[] businessObjects, String propertyInArray, String label, boolean isVertical) {
    return this.getCurrentPanelAndUseLine().addRadioGroup(businessObject, property, businessObjects, propertyInArray, label, isVertical);
  }
  
  public ListModel addRadioGroup(BusinessObject businessObject, String property, List<?> choices, String label, boolean isVertical) {
    return this.getCurrentPanelAndUseLine().addRadioGroup(businessObject, property, choices, label, isVertical);
  }
  
  public ListModel addRadioGroup(BusinessObject businessObject, String property, Object[] choices, String label, boolean isVertical) {
    return this.getCurrentPanelAndUseLine().addRadioGroup(businessObject, property, choices, label, isVertical);
  }
  
  public ListModel addListBox(BusinessObject businessObject, String property, BusinessObject[] businessObjects, String propertyInArray, String label) {
    return this.getCurrentPanelAndUseLine().addListBox(businessObject, property, businessObjects, propertyInArray, label);
  }
  
  public ListModel addEnumListBox(BusinessObject businessObject, String property, ZenEnum[] choices, String label) {
    return this.getCurrentPanelAndUseLine().addEnumListBox(businessObject, property, choices, label);
  }
  
  public ListModel addListBox(BusinessObject businessObject, String property, List<BusinessObject> businessObjects, String propertyInList, String label) {
    return this.getCurrentPanelAndUseLine().addListBox(businessObject, property, businessObjects, propertyInList, label);
  }
  
  public ListModel addListBox(BusinessObject businessObject, String property, List<?> choices, String label) {
    return this.getCurrentPanelAndUseLine().addListBox(businessObject, property, choices, label);
  }
  
  public ListModel addListBox(BusinessObject businessObject, String property, Object[] choices, String label) {
    return this.getCurrentPanelAndUseLine().addListBox(businessObject, property, choices, label);
  }
  
  private VerticalUserFormPanel getCurrentPanel() {
    int currentColumn = this.currentInputRow / this.nbInputRows;
    VerticalUserFormPanel currentPanel = this.subpanels[currentColumn];
    return currentPanel;
  }
  
  private VerticalUserFormPanel getCurrentPanelAndUseLine() {
    VerticalUserFormPanel formPanel = this.getCurrentPanel();
    this.currentInputRow++;
    return formPanel;
  }
  
}
